$(function () {
    // 必须含有车间编码
    workshopCode = getQueryString('code');
    if (!workshopCode) {
        return;
    }
    $("#jqGrid").Grid({
        url: '../device/stat/ticket?workshopCode=' + workshopCode,
        multiselect: false,
        rownumWidth: 35,
        rowNum: 100,
        colModel: [
            {label: '工单类别', name: 'ticketType',width: 80},
            {label: '产线', name: 'line'},
            {label: '工序', name: 'process'},
            {label: '设备名称', name: 'devName'},
            {label: '责任人', name: 'executor',width: 80},
            {label: '工单状态', name: 'statusDesc',width: 80},
            {label: '报修时长', name: 'callRepairDuration',width: 80},
            {name: 'urgeStatus',hidden: true}
        ],
        gridComplete: function () {
            var ids = $('#jqGrid').getDataIDs();
            for (var i = 0; i < ids.length; i++) {
                var rowData = $('#jqGrid').getRowData(ids[i]);
                if (rowData.urgeStatus && rowData.urgeStatus == "1") {
                    $('#' + ids[i]).find("td").addClass("urgeCss");
                }
            }
            var ob3 = $(this).getGridParam("records");
            var div = $('#jqGrid').closest('.ui-jqgrid-bdiv')[0],
                offsetHeight = div.offsetHeight, scrollHeight = div.scrollHeight-offsetHeight, scrollTop = 0,timer;
            function scrollBody() {
                timer = setInterval(function () {
                    scrollTop += 1;
                    div.scrollTop = scrollTop;
                    if (scrollTop >= scrollHeight) {//循环滚动
                        scrollHeight = div.scrollHeight - offsetHeight;//如果在滚动过程中添加过数据，需要重新结算scrollHeight
                        if(scrollTop>=(ob3-12)*33){
                            // scrollTop = 0; //循环滚动，重置滚动头即可
                            // 滑动到底部后重新加载页面
                            location.reload();
                        }
                    }
                }, 100);
            }
            // 超过12条，则滚动，否则则刷新页面
            if(ob3>12){
                scrollBody() ;///开始滚动jqgrid内容
            }else{
                setInterval(function () {
                        location.reload();
                }, 10000);
            }
        }
    });
    //自应高度
    $(window).resize(function () {
        $('#jqGrid').setGridHeight($(window).height() - 178);
    });
    vm.search();
});

var vm = new Vue({
    el: "#rrapp",
    data: {
        //报修工单统计
        workOrderStat: {
            wd:0,
            un: 0,
            ing: 0,
            ed: 0
        },
        // 维护计划统计
        maintainPlanStat: {
            wd:0,
            un: 0,
            ing: 0,
            ed: 0
        }
    },
    methods: {
        search: function () {
            // 加载当前组织下的报修工单和维护计划执行情况
            Ajax.request({
                url: "../device/stat/maintainNum?lineCode=" + workshopCode,
                type: "GET",
                contentType: "application/json",
                successCallback: function (res) {
                    if (res.code == 0) {
                        vm.workOrderStat = res.deptStat.workOrderStat;
                        vm.maintainPlanStat = res.deptStat.maintainPlanStat;
                    } else {
                        iview.Message.error(r.msg);
                    }
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                search: true,
                postData: {},
                page: page
            }).trigger("reloadGrid", [{ page: 1}]);
        }
    }
});
